/*---------------------------------------------------------------------------------------------------------
 *
 *   Developer: 寤夌涓�[Jack路Lian]
 * Create Date: 2011-5-1 涓婂崍10:00:00
 * Description: 1.JSON宸ュ叿绫�
 *              2.
 *              3.
 *   Copyright (C) 2008 Jack路Lian. All rights reserved.
 *
 *---------------------------------------------------------------------------------------------------------
 */

package com.letou.ls.uitl;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

/**
 * @author 寤夌涓�(Jack路Lian)
 * @date: 2013-3-1 涓嬪崍00:00:00
 * @desc: 1.<br />
 *        2.
 */
public final class JsonUtil {

	private static final ObjectMapper mapper = new ObjectMapper();

	/**
	 * bean 杞�JSON 瀛楃涓�
	 * 
	 * @param <T>
	 *            娉涘瀷澹版槑
	 * @param bean
	 *            绫荤殑瀹炰緥
	 * @return JSON瀛楃涓�
	 */
	public static <T> String toJson(T bean) {
		StringWriter sw = new StringWriter();
		JsonGenerator gen = null;
		if (null == bean) {
			sw.append("{}");
			return sw.toString();
		}
		try {
			gen = new JsonFactory().createJsonGenerator(sw);
			mapper.writeValue(gen, bean);
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				gen.flush();
				gen.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sw.toString();
	}

	/**
	 * JSON 杞寚瀹氱殑bean瀵硅薄
	 * 
	 * @param <T>
	 *            娉涘瀷澹版槑
	 * @param json
	 *            JSON瀛楃涓�
	 * @param clzz
	 *            瑕佽浆鎹㈠璞＄殑绫诲瀷
	 * @return
	 */
	public static <T> T fromJson(String json, Class<T> clzz) {
		T t = null;
		try {
			t = mapper.readValue(json, clzz);
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return t;
	}

	/**
	 * JSON 杞寚瀹氱殑bean瀵硅薄
	 * 
	 * @param <T>
	 *            娉涘瀷澹版槑
	 * @param json
	 *            JSON瀛楃涓�
	 * @param typeRef
	 *            (TypeReference) 瑕佽浆鎹㈠璞＄殑绫诲瀷
	 * @return
	 */
	public static <T> T fromJson(String json, TypeReference<T> typeRef) {
		T t = null;
		try {
			t = mapper.readValue(json, typeRef);
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return t;
	}

	/**
	 * JSON 杞�Map
	 * 
	 * @param json
	 *            JSON瀛楃涓�璇蜂繚鎸乲ey鏄姞浜嗗弻寮曞彿鐨�
	 * @return Map瀵硅薄,榛樿涓篐ashMap
	 */
	public static Map<?, ?> fromJson(String json) {
		Map<?, ?> map = null;
		try {
			map = mapper.readValue(json, HashMap.class);
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * JSON 杞寚瀹氱殑bean瀵硅薄
	 * 
	 * @param <T>
	 *            娉涘瀷澹版槑
	 * @param jsonFile
	 *            JSON鏂囦欢
	 * @param clzz
	 *            瑕佽浆鎹㈠璞＄殑绫诲瀷
	 * @return
	 */
	public static <T> T fromFile(File jsonFile, Class<T> clzz) {
		T t = null;
		try {
			t = mapper.readValue(jsonFile, clzz);
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return t;
	}

	/**
	 * JSON 杞�Map
	 * 
	 * @param jsonFile
	 *            JSON,璇蜂繚鎸乲ey鏄姞浜嗗弻寮曞彿鐨�
	 * @return Map瀵硅薄,榛樿涓篐ashMap
	 */
	public static Map<?, ?> fromFile(File jsonFile) {
		Map<?, ?> map = null;
		try {
			map = mapper.readValue(jsonFile, HashMap.class);
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return map;
	}

}
